package com.cn.conf

import com.cn.conf.handler.ApiInterceptor
import com.cn.conf.handler.UserInfoResolver
import org.springframework.context.annotation.Configuration
import org.springframework.web.method.support.HandlerMethodArgumentResolver
import org.springframework.web.servlet.config.annotation.CorsRegistry
import org.springframework.web.servlet.config.annotation.InterceptorRegistry
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import javax.annotation.Resource


@Configuration
class WebConfig() : WebMvcConfigurer {
    @Resource
    lateinit var apiInterceptor: ApiInterceptor

    @Resource
    lateinit var userInfoResolver: UserInfoResolver

    override fun addInterceptors(registry: InterceptorRegistry) {
        registry.addInterceptor(apiInterceptor)
            .addPathPatterns("/**/**")
    }

    override fun addArgumentResolvers(resolvers: List<HandlerMethodArgumentResolver?>) {
        resolvers.plus(userInfoResolver)
    }

    override fun addCorsMappings(registry: CorsRegistry) {
        registry.addMapping("/**")
            .allowedOriginPatterns("*")
            .allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH")
            .allowedHeaders("*")
            .allowCredentials(true)
            .maxAge(3600);
    }

}